// 定义和ai相关的接口
const Router = require('koa-router')
const router = new Router()
const { verify } = require('../utils/jwt.js')
const { getFoodComponent, getSportColorie } = require('../ai/index.js')

router.prefix('/ai')

// 获取食物成分
router.get('/getFoodComponent', verify(), async (ctx, next) => {
    const { foodType } = ctx.query
    // console.log(foodType);
    const res = await getFoodComponent(foodType)
    // console.log(res);
    try {
        if (res.length) {
            ctx.body = {
                code: 200,
                data: res,
                msg: '获取成功',
            }
        } else {
            ctx.body = {
                code: 404,
                data: [],
                msg: '未找到相关食物成分',
            }
        }
    } catch (error) {
        ctx.body = {
            code: 500,
            data: error,
            msg: '服务器异常',
        }
    }
})

// 获取一项运动大概消耗多少热量
router.post('/getSportColorie', verify(), async (ctx, next) => {
    const type = ctx.request.body
    // console.log(type);
    const res = await getSportColorie(type)
    // console.log(res);
    try {
        if (res) {
            ctx.body = {
                code: 200,
                data: res,
                msg: '获取成功',
            }
        } else {
            ctx.body = {
                code: 404,
                data: [],
                msg: '获取失败',
            }
        }
    } catch (error) {
        ctx.body = {
            code: 500,
            data: error,
            msg: '服务器异常',
        }
    }
})

module.exports = {
    aiRouter: router
}